<template>
  <div class="wrap">
    <div style="margin-bottom:10px;">
      <el-date-picker
        :editable="false"
        v-model="dateValue"
        style="width:150px;"
        type="month"
        value-format="yyyy-MM-dd"
        :picker-options="pickerOptions"
        placeholder="选择日期"
        @change="dateChange">
      </el-date-picker>
      <!-- <el-select v-model="systemValue" placeholder="系统" class="select" style="width:150px;" @change="systemChange">
        <el-option
          v-for="item in systemOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select> -->
      <!-- <el-switch
        v-model="newUserSwitch"
        active-text="仅看新用户"
        @change="newUserSwitchChange">
      </el-switch> -->
      <el-button icon="el-icon-refresh" @click="refreshClick" style="margin-left:20px;">刷新</el-button>
    </div>
    <el-table
      class="table"
      border fit highlight-current-row
      v-loading="loading"
      element-loading-text="加载中"
      element-loading-spinner="el-icon-loading"
      :data="tableData"
      style="width: 100%">
    <el-table-column
          align="center"
           width="200"
          label="日期">
          <template slot-scope="scope">
            {{_mapping.date_format2(scope.row.createTime)}}
          </template>
      </el-table-column>
    
      <el-table-column align="center" label="渠道">
        <el-table-column
          align="center"
          :label="`注册用户|应结|扣量比`"
          :render-header="renderHeader2">
          <template slot-scope="scope">
            <span>{{scope.row.channelRegisterNum}} | {{scope.row.channelRealRegisterNum}} | {{ scope.row.channelRegisterNum===0 ? '-': (((scope.row.channelRegisterNum - scope.row.channelRealRegisterNum)*100/scope.row.channelRegisterNum).toFixed(2))+'%' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="`应结金额 ${this.tab.channelSettlementMoney || 0}`"
          :render-header="renderHeader2">
          <template slot-scope="scope">
            <span>{{scope.row.channelSettlementTotalMoney}}</span>
          </template>
        </el-table-column>
      </el-table-column>

       <el-table-column align="center" label="贷超">
        <el-table-column
          align="center"
          :label="`预计注册|应结注册|应结UV|应结下款`"
          :render-header="renderHeader4">
          <template slot-scope="scope">
            <span>{{scope.row.productPbRegisterNum}} | {{scope.row.productRealRegisterNum}} | {{scope.row.productRealUvNum}} | {{scope.row.productRealCpsNum}}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="`预计金额|应结金额|毛利|毛利率 ${this.tab.productPbMoney || 0} | ${this.tab.productRealMoney || 0} | ${this.tab.maoliTotal || 0} `"
          :render-header="renderHeader4">
          <template slot-scope="scope">
            <span>{{scope.row.productPbTotalMoney}} | {{scope.row.productRealTotalMoney}} | {{scope.row.productRealTotalMoney - scope.row.channelSettlementTotalMoney}} | {{ scope.row.productRealTotalMoney === 0? '-' :(((scope.row.productRealTotalMoney - scope.row.channelSettlementTotalMoney)*100/scope.row.productRealTotalMoney).toFixed(2))+'%' }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column
          align="center"
          :label="`已结金额|未结金额`"
          :render-header="renderHeader4">
          <template slot-scope="">
            <span></span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="`毛利|毛利率`"
          :render-header="renderHeader4">
          <template slot-scope="">
            <span></span>
          </template>
        </el-table-column> -->
    </el-table-column>
     <!-- <el-table-column
          align="center"
          width="80"
          prop=""
          label="总毛利">
      </el-table-column> -->


    </el-table>
    <div class="clearfix pagination" style="margin-top:30px;">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.pageNum"
          :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalNum">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import _finace from'@/services/dailyStatistics.js'
export default {
  data() {
    return {
      newUserSwitch: false,
     
      dateValue: '',
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        }
      },
      query: {
        pageNum: 1,
        pageSize: 30
      },
      totalNum: 0,
      tab: {
        registerPayTotal: 0,
      
      },
      channelSettlementMoney :0,
      productPbMoney: 0,
      productRealMoney  : 0 ,
      maoliTotal :0,
      loading: false,
      tableData: []
    }
  },
  methods: {
    handleSizeChange(val) {
      this.query.pageSize = val
      this.getStatistic()
    },
    handleCurrentChange(val) {
      this.query.pageNum = val
      this.getStatistic()
    },
    // 仅看新用户开关
    newUserSwitchChange(val){
      this.getStatistic()
    },
    // 切换系统
    // systemChange(val){
    //   console.log(this.systemValue)
    // },
    renderHeader2(createElement, { column, _self }) {
      console.log(createElement) //  function (a, b, c, d) { return createElement(vm, a, b, c, d, true); };
      const label = column.label     
      const labelArr = label.split(' ') 
      console.log('labelArr', labelArr)
      return createElement(
        'span',     //创建最外层的标签可随意
        [
          createElement('span', {  // 创建第一个元素的标签可随意
            attrs: { type: 'text' } 
          },[labelArr[0]]), 
          createElement('p', {  // 创建第二个元素的标签可随意 
            attrs: { type: 'text', style: 'font-size:12px; font-weight:bold; color: #999999;' } // 给分割的某个元素单独加样式
          }, [labelArr[1], ' ', labelArr[2], ' ', labelArr[3]])
        ]
      )
    },
    renderHeader3(createElement, { column, _self }) {
      console.log(createElement) //  function (a, b, c, d) { return createElement(vm, a, b, c, d, true); };
      const label = column.label     
      const labelArr = label.split(' ') 
      console.log('labelArr', labelArr)
      return createElement(
        'span',     //创建最外层的标签可随意
        [
          createElement('span', {  // 创建第一个元素的标签可随意
            attrs: { type: 'text' } 
          },[labelArr[0]]), 
          createElement('p', {  // 创建第二个元素的标签可随意 
            attrs: { type: 'text', style: 'font-size:12px; font-weight:bold; color: #999999;' } // 给分割的某个元素单独加样式
          }, [labelArr[1], ' ', labelArr[2], ' ', labelArr[3], ' ', labelArr[4], ' ', labelArr[5]])
        ]
      )
    },
    renderHeader4(createElement, { column, _self }) {
      console.log(createElement) //  function (a, b, c, d) { return createElement(vm, a, b, c, d, true); };
      const label = column.label     
      const labelArr = label.split(' ') 
      console.log('labelArr', labelArr)
      return createElement(
        'span',     //创建最外层的标签可随意
        [
          createElement('span', {  // 创建第一个元素的标签可随意
            attrs: { type: 'text' } 
          },[labelArr[0]]), 
          createElement('p', {  // 创建第二个元素的标签可随意 
            attrs: { type: 'text', style: 'font-size:12px; font-weight:bold; color: #999999;' } // 给分割的某个元素单独加样式
          }, [labelArr[1], ' ', labelArr[2], ' ', labelArr[3], ' ', labelArr[4], ' ', labelArr[5], ' ', labelArr[6], ' ', labelArr[7]])
        ]
      )
    },
    // 日期改变
    dateChange(val){
      console.log('dateValue', this.dateValue)
      this.getStatistic()
    },
    // 刷新按钮
    refreshClick(){
      this.getStatistic()
    },
    getStatistic(){
      this.loading = true
      let params = {
        "isNewUser": this.newUserSwitch ? 1 : 0,
        "pageNum": this.query.pageNum,
        "pageSize": this.query.pageSize,
        // "platform": "string",
        "searchDate": this.dateValue
      }
      _finace.getDaliyStatistic(params)
      .then(res => {
        this.tableData = res.data.list || []
        this.totalNum = res.data.total
        this.tab.channelSettlementMoney = 0
        this.tab.productPbMoney = 0
        this.tab.productRealMoney = 0 
        this.tab.maoliTotal = 0
        if(this.tableData.length > 0){
          this.tableData.forEach((item, index) => {
            this.tab.channelSettlementMoney  += Number(item.channelSettlementTotalMoney)
            this.tab.productPbMoney += Number(item.productPbTotalMoney)
            this.tab.productRealMoney += Number(item.productRealTotalMoney)
          })
          this.tab.maoliTotal =  this.tab.productRealMoney -  this.tab.channelSettlementMoney
        }
        this.loading = false
        console.log('res', res.data)
      })
    }
  },
  created(){
    this.dateValue = this._mapping.date_format2(new Date())
    this.getStatistic()
  }
}
</script>
<style lang="scss" scoped>
</style>